/* *******************************
 * NewJavaEditorMenuItemAction.java
 * Mrogrape Notepad
 * *******************************
 * 添加用户输入类名检查。
 * 添加创建类向导。
 * 
 */

package com.mrogrape.notepad.activity.action.item;

import java.awt.Component;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;

import com.mrogrape.notepad.activity.MainFrame;
import com.mrogrape.notepad.common.ViewUtils;
import com.mrogrape.notepad.ui.editor.ContentType;
import com.mrogrape.util.StringUtils;


public class NewJavaEditorMenuItemAction extends AbstractAction {

    /** serialVersionUID */
    private static final long serialVersionUID = -1L;

    public NewJavaEditorMenuItemAction(MainFrame frame){

	this.frame = frame;
	super.putValue(Action.NAME, "Java");
	super.putValue(Action.SHORT_DESCRIPTION, "Create a new Java editor.");
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {

	String input = ViewUtils.showInputDialog((Component) this.frame, "创建新的Java编辑器", "输入一个类名");

	if(StringUtils.isNotEmpty(input)){

	    String title = null;
	    // 检查用户是否输入了扩展名。
	    if(this.checkIsJavaCodeFileName(input)){

		title = input;
	    }
	    else{

		title = this.createJavaClassFileName(input);
	    }

	    boolean checkResult = this.frame.checkTitleUseable(title);
	    if(checkResult){
		this.frame.createNewEditor(ContentType.JAVA,title);
	    }
	    else{

		ViewUtils.showErrorMessageDialog((Component) this.frame, "已经存在名为“"+input+"”的Java编辑器。");
	    }
	}
	else{

	    ViewUtils.showInformationDialog((Component) this.frame, "未输入类名。");
	}

    }

    public String createJavaClassFileName(String className){

	return className + ".java";
    }

    public boolean checkIsJavaCodeFileName(String name){

	return name.toLowerCase().endsWith(".java");
    }

    private MainFrame frame;
}
